package cn.neo.hhsharding.strategy;


import groovy.lang.Closure;

import java.util.List;
import java.util.Map;
import java.util.Properties;

public class HashShardingStrategy implements ShardingStrategy {

    private final String shardingColumn;

    private final String algorithmExpression;

    public HashShardingStrategy(Properties properties) {
        this.shardingColumn = properties.getProperty("shardingColumn");
        this.algorithmExpression = properties.getProperty("algorithmClassName");
    }


    @Override
    public List<String> getShardingColums() {
        return List.of(this.algorithmExpression);
    }

    @Override
    public String doSharding(List<String> availableTargetNames, String logicTableName, Map<String, Object> shardingParams) {
        String expression = InlineExpressionParser.handlePlaceHolder(algorithmExpression);
        InlineExpressionParser parser = new InlineExpressionParser(expression);
        Closure<?> closure = parser.evaluateClosure();
        closure.setProperty(this.shardingColumn, shardingParams.get(this.shardingColumn));

        return closure.call().toString();
    }
}
